package com.fenghongzhang.day0102;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class Main3Activity extends AppCompatActivity {
    private GridView grid;

    private List<Pic> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        grid = (GridView) findViewById(R.id.grid);

        list = new ArrayList<>();

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},999);
        }

        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                View inflate = LayoutInflater.from(Main3Activity.this).inflate(R.layout.dialog_layout, null);

                ImageView viewById = inflate.findViewById(R.id.dig_img);
                viewById.setImageBitmap(BitmapFactory.decodeFile(list.get(position).getData()));

                AlertDialog.Builder builder = new AlertDialog.Builder(Main3Activity.this);
                builder.setView(inflate);
                final AlertDialog alertDialog = builder.create();

                alertDialog.show();
                viewById.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog.dismiss();
                    }
                });

            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 999 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            ContentResolver contentResolver = getContentResolver();
            Cursor query = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
            if(query !=null){
                while(query.moveToNext()){
                    String title = query.getString(query.getColumnIndex(MediaStore.Images.Media.TITLE));
                    String data = query.getString(query.getColumnIndex(MediaStore.Images.Media.DATA));

                    Pic pic = new Pic(title,data);
                    list.add(pic);




                    MyAdapter myAdapter = new MyAdapter(list, this);
                    grid.setAdapter(myAdapter);
                }
                query.close();
            }
        }else{
            finish();

        }
    }
}
